package com.heba.greenmulu.login;

import android.os.Bundle;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.heba.greenmulu.BaseActivity;
import com.heba.greenmulu.R;
import com.heba.helper.MemberShipHelper;
import com.heba.utils.CommonUtils;
import com.heba.utils.DensityUtils;
import com.heba.utils.StatusBarUtils;
import com.heba.utils.ToastShow;

/**
 * 忘记密码页面
 * Created by jason on 2019/2/12.
 */

public class PasswordForgetActivity extends BaseActivity {

    private ConstraintLayout topbar;
    private EditText edit_email;
    private EditText edit_code;
    private EditText edit_password;
    private TextView txt_obtain_code;
    private Button btn_confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置状态栏透明
        StatusBarUtils.setStatusBar(this);
        setContentView(R.layout.activity_password_forget);
        initTopbar();
        findViewById();
        setTopbarPadding();
        initView();
    }

    @Override
    protected void findViewById() {
        topbar = findViewById(R.id.topbar);
        edit_email = findViewById(R.id.edit_email);
        edit_code = findViewById(R.id.edit_code);
        edit_password = findViewById(R.id.edit_password);
        txt_obtain_code = findViewById(R.id.txt_obtain_code);
        btn_confirm = findViewById(R.id.btn_confirm);
    }

    @Override
    protected void initView() {
        txt_title.setText(getResources().getString(R.string.password_forget_s));

        /**
         * 获取邮箱验证码
         */
        txt_obtain_code.setOnClickListener(v -> {
            getEmailCode();
        });

        /**
         * 确认
         */
        btn_confirm.setOnClickListener(v -> {
            forgetPassword();
        });
    }

    /**
     * Let topbar to extend to status bar.
     *
     * @notice this method have to be used after setContentView.
     */
    private void setTopbarPadding() {
        // Set the padding to match the Status Bar height
        topbar.setPadding(0, StatusBarUtils.getStatusBarHeight(this), 0, 0);
    }

    /**
     * 获取邮箱验证码
     */
    private void getEmailCode() {
        DensityUtils.hideSoftWindow(this, edit_email);
        if (edit_email.getText().toString().isEmpty() || !CommonUtils.isEmail(edit_email.getText().toString())) {
            ToastShow.displayToast(this, getResources().getString(R.string.input_email_format));
            return;
        }
        progressDialog.show();
        new MemberShipHelper(new MemberShipHelper.MembershipListener() {
            @Override
            public void Success() {
                progressDialog.dismiss();
                txt_obtain_code.setVisibility(View.INVISIBLE);
                ToastShow.displayToast(PasswordForgetActivity.this, getResources().getString(R.string.check_email));
            }

            @Override
            public void Fail(String error) {
                progressDialog.dismiss();
            }
        }).getEmailCode(this, edit_email.getText().toString());
    }

    /**
     * 重置密码
     */
    private void forgetPassword() {
        DensityUtils.hideSoftWindow(this, edit_email);
        if (edit_email.getText().toString().isEmpty() || edit_code.getText().toString().isEmpty() || edit_password.getText().toString().isEmpty()) {
            ToastShow.displayToast(this, getResources().getString(R.string.sign_up_message_tips));
            return;
        }
        if (!CommonUtils.isEmail(edit_email.getText().toString())) {
            ToastShow.displayToast(this, getResources().getString(R.string.input_email_format));
            return;
        }
        progressDialog.show();
        new MemberShipHelper(new MemberShipHelper.MembershipListener() {
            @Override
            public void Success() {
                progressDialog.dismiss();
                finish();
                ToastShow.displayToast(PasswordForgetActivity.this, getResources().getString(R.string.update_success));
            }

            @Override
            public void Fail(String error) {
                progressDialog.dismiss();
            }
        }).resetEmailPassword(this, edit_email.getText().toString(), edit_password.getText().toString(), edit_code.getText().toString());
    }
}
